package com.loiot.baqi.service.job;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.loiot.baqi.commons.message.email.EmailClient;
import com.loiot.baqi.commons.message.email.SimpleEmailVo;
import com.loiot.baqi.constant.ApplicationConst;
import com.loiot.baqi.dao.AccountDao;
import com.loiot.baqi.dao.ZpCompanyJobInfoDao;
import com.loiot.baqi.dao.ZpDictionaryInfoDao;
import com.loiot.baqi.dao.ZpJlInfoDao;
import com.loiot.baqi.pojo.ZpCompanyJobInfo;
import com.loiot.baqi.pojo.ZpDictionaryInfo;
import com.loiot.baqi.status.AccountType;
import com.loiot.baqi.status.CompanyJobSourceType;
import com.loiot.baqi.status.HeartbeatJobType;
import com.loiot.baqi.status.JlAuditType;
import com.loiot.baqi.status.PauseStartType;
import com.loiot.baqi.status.RecommendFlowType;
import com.loiot.baqi.utils.HtmlUnitUtil;
import com.loiot.baqi.utils.JsonToObject;
import com.loiot.baqi.utils.JsonUtils;
import com.loiot.baqi.utils.MD5Util;
import com.loiot.baqi.vo.hd.Root;
import com.loiot.commons.utils.DateUtil;
import com.loiot.commons.utils.StringUtil;
import com.timeloit.pojo.Account;
/**
 * 统计薪水 给薪水管理员
 * @author Administrator
 *
 */
@Service("nodifyFinanceJob")
@Scope("prototype")
public class NodifyFinanceJob extends JobTaskDefine {

	@Resource
	private ZpCompanyJobInfoDao zpCompanyJobInfoDao;
	@Resource
	private ZpDictionaryInfoDao zpDictionaryInfoDao;
	
	@Resource
	private ZpJlInfoDao zpJlInfoDao;
	
	 
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	/**
     * 账号数据访问接口
     */
    @Resource
    private AccountDao accountDao;
	  
	
	@Override
	public void doTask() {
		try {
			nodifyFinanceJob();
    	} catch (Exception e) {
			this.exceptionContent=e.getMessage();
		}finally{
			//添加心跳
			this.jobType=(int)HeartbeatJobType.NODIFY_FINANCE_JOB.getCode();
			this.addHeartbeat();
		}	
	}
	
	public void nodifyFinanceJob() throws Exception{
		String emailMessage="";
    	String auditPassMessage = getAuditPassPerson();
    	String goInterviewMessage = getAlreadyGoInterviewPerson();
    	if(auditPassMessage.length()>0){
    		emailMessage=auditPassMessage;
    	}
    	if(goInterviewMessage.length()>0){
    	   if(emailMessage.length()==0){
    		   emailMessage=auditPassMessage;
    	   }  else{
    		   emailMessage=emailMessage+"\n"+goInterviewMessage;
    	   } 
    	}
    	//没有薪水发放
    	if(emailMessage.length()==0){
    		emailMessage=ApplicationConst.getMessage("10106");
    	}
    	System.out.println("emailContent:"+emailMessage);

    	Account account=this.getSalaryManager();
    	//向薪水管理员发送邮件
		if(account!=null && account.getEmail()!=null && StringUtil.isEmail(account.getEmail()) ){
			 SimpleEmailVo vo = new SimpleEmailVo();
			 vo.addEmail(account.getEmail(),"273035735@qq.com");
            vo.setTitle("憬仪[红包]发放通知");
            vo.setContent(emailMessage);
            emailClient.sendT(vo);
            log.info("发送时间："+DateUtil.toString(DateUtil.getNow(), DateUtil.DEFAULT_LONG_FORMAT)); 
		}
	}
	
	
	  //获得技术评审通过的信息
    public  String getAuditPassPerson()throws Exception{
    	
         
    	HashMap<String, Object> pMap = new HashMap<String, Object>();
    	pMap.put("auditTypeId", JlAuditType.AUDIT_OK.getCode());
    	pMap.put("qtype","one");
    	pMap.put("endTime",new Date());
    	pMap.put("startTime",DateUtil.addDays(new Date(), -1));
    	
    	List<HashMap<String, Object>> list = this.zpJlInfoDao.statisticsJlInfo(pMap);
    	String emailContent="";
    	if(list!=null && list.size()>0)
    	{
    		
    		List<ZpDictionaryInfo> dicList = zpDictionaryInfoDao.queryZpDictionaryInfoList();
    		for(HashMap<String,Object> map : list){
    			
    			String realName =String.valueOf(map.get("realName"));	
    			int jlcount =Integer.parseInt(String.valueOf(map.get("jlcount")));	
    			String names =String.valueOf(map.get("names"));	
    			String positions =String.valueOf(map.get("positions"));	
    			String name[]=names.split(",");
    			String position[]=positions.split(",");
    			String nameStr="";
    			for(int i=0;i<name.length;i++){
    				if(i==0){
        				nameStr=name[i]+" "+getDicName(Long.parseLong(position[i]),dicList); 
    				} else{
        				nameStr=nameStr+" 、"+name[i]+" "+getDicName(Long.parseLong(position[i]),dicList); 
    				}
    			}
    			emailContent=emailContent+" \n <br> "+ApplicationConst.getMessage("10104", realName,String.valueOf(jlcount),nameStr);
    			
    			//emailContent=emailContent+emailContent;
    		}
    	}
    	return emailContent;
    }
    
    //已经去面试的信息
    public  String getAlreadyGoInterviewPerson()throws Exception{
    	
    	HashMap<String, Object> pMap = new HashMap<String, Object>();
    	pMap.put("qtype","two");
    	pMap.put("hunterGotoInterviewStatus",RecommendFlowType.HUNTER_ALREAD_GOTO_INTERVIEW.getCode());
    	pMap.put("endTime",new Date());
    	pMap.put("startTime",DateUtil.addDays(new Date(), -1));
    	List<HashMap<String, Object>> list = this.zpJlInfoDao.statisticsJlInfo(pMap);
    	String emailContent="";
    	if(list!=null && list.size()>0)
    	{
    		List<ZpDictionaryInfo> dicList = zpDictionaryInfoDao.queryZpDictionaryInfoList();
    		for(HashMap<String,Object> map : list){
    			String realName =String.valueOf(map.get("realName"));	
    			String jlName =String.valueOf(map.get("name"));	
    			Long jobPositionId =Long.parseLong(String.valueOf(map.get("jobPositionId")));	
    			if(emailContent.length()==0){
    				emailContent=ApplicationConst.getMessage("10105", realName,jlName+" ("+getDicName(jobPositionId, dicList)+")");
    			} else{
    				emailContent=emailContent+" \n <br> " +ApplicationConst.getMessage("10105", realName,jlName+" ("+getDicName(jobPositionId, dicList)+")");
    			}
    		}
    	}
    	
    	//System.out.println(emailContent);
    	return emailContent;
    }
    
    //获取薪水管理员对接人
    public Account getSalaryManager(){
		 HashMap<String,Object> pmap = new HashMap<String,Object>();
		 pmap.put("type", AccountType.SALARY_MANAGER.getCode());
		 pmap.put("isDelete", PauseStartType.START.getCode());
    	 List<Account> list = accountDao.queryAccountList(pmap);
    	 if(list!=null && list.size()>0){
    		return list.get(0);
    	 }
    	 return null;
    }
    
    public  String getDicName(Long code,List<ZpDictionaryInfo> list){
		if(code==null){
			return "";
		}
		String name ="";
		for(ZpDictionaryInfo dic : list){
			//System.out.println(code+"=" + dic.getDictionaryId()  +"结果:"+(dic.getDictionaryId()==code));
			if(dic.getDictionaryId().intValue()==code.intValue()){
				name=dic.getShowName();
				break;
			}
		}
		return name;
	}

}
